﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyOA.BLL;

namespace MyOA.UI.PersonManage
{
    public partial class DepartManage : System.Web.UI.Page
    {
        DepartInfoManager departInfoManger = new DepartInfoManager();
        UserInfoManager userInfoManager = new UserInfoManager();
        string message = string.Empty;

        private int Pager
        {
            set { ViewState["page"] = value; }
            get { return (int)ViewState["page"]; }
        }

        //申明提示信息
        protected void Page_Load(object sender, EventArgs e)
        {
            //未登录先登录
            if (Session["loginId"] == null)
            {
                Response.Redirect("~/Login.aspx");
            }
            //登录加载
            else
            {
                if (!IsPostBack)
                {
                    Pager = 1;

                    dataBind();
                }
            }
        }
        //显示部门信息
        public void DisplayDepartInfo()
        {
            this.gvDepartInfo.DataSourceID = null;
            this.gvDepartInfo.DataSource = departInfoManger.GetAllDepartInfo();
            this.gvDepartInfo.DataBind();
        }
        //添加按钮
        protected void btnAddUser_Click(object sender, EventArgs e)
        {
            Response.Redirect("CreateOrUpdateDepartInfo.aspx");
        }
        //数据绑定事件
        protected void gvDepartInfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "color=this.style.backgroundColor;this.style.backgroundColor='#6699ff'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=color");
                ImageButton imgbtnDelete = (ImageButton)e.Row.FindControl("imgbtnDelete");
                imgbtnDelete.Attributes.Add("onclick", "return confirm('确定删除吗')");
            }
        }
        //操作删除或修改部门信息
        protected void gvDepartInfo_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string commandName = e.CommandName;
            int departId = Convert.ToInt32(e.CommandArgument);
            if (commandName == "departInfoUpdate")
            {
                Response.Redirect("CreateOrUpdateDepartInfo.aspx?DepartId=" + departId);
            }
            if (commandName == "departInfoDelete")
            {
                if (userInfoManager.GetCountUserInfoByDepartId(departId) == 0)
                {
                    int result = departInfoManger.DeleteDepartInfoByDepartId(departId);
                    if (result > 0)
                    {
                        message = "删除成功";
                    }
                    else
                    {
                        message = "删除失败";
                    }
                }
                else
                {
                    message = "存在关联信息，请查看后再删除";
                }
            }
            string script = string.Format("<script>Alert('{0}',null,null,'温馨提示','href()');</script>", message);
            ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", script, false);
            dataBind();
        }

        protected void anpDepartInfo_PageChanged(object src, EventArgs e)
        {
            Pager = anpDepartInfo.CurrentPageIndex;
            dataBind();
        }
        //数据绑定方法
        private void dataBind()
        {
            //实例化数据化对象
            PagedDataSource pgDataSource = new PagedDataSource();
            pgDataSource.DataSource = departInfoManger.GetAllDepartInfo();
            anpDepartInfo.RecordCount = departInfoManger.GetAllDepartInfo().Count();
            pgDataSource.AllowPaging = true;    //启用分页
            pgDataSource.PageSize = 5;  //单页显示的项数
            pgDataSource.CurrentPageIndex = Pager - 1;  //获取当前的索引
            this.gvDepartInfo.DataSourceID = null;
            this.gvDepartInfo.DataSource = pgDataSource;
            this.gvDepartInfo.DataBind();

        }
    }
}
